package com.woniuxy.controller;

import com.woniuxy.pojo.SysPermission;
import com.woniuxy.pojo.dto.AddPermissionDTO;
import com.woniuxy.pojo.dto.PermissionTree;
import com.woniuxy.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class SysPermissionController {
    @Autowired
    SysPermissionService permissionService;

    @GetMapping("/test")
    public ResponseEntity<?> test(){
        return ResponseEntity.ok("test");
    }

    @GetMapping("/getPermissionForest")
    public ResponseEntity<?> getPermissionForest(){
        List<PermissionTree> forest = permissionService.getPermissionForest();
        return ResponseEntity.ok(forest);
    }

    @PostMapping("/addLevel1Perm")
    public ResponseEntity addLevel1Perm(@RequestBody AddPermissionDTO dto){
        Boolean success = permissionService.addLevel1Perm(dto);
        return ResponseEntity.ok(success);

    }

    @DeleteMapping("/deletePermission/{id}")
    public ResponseEntity<?> deletePermission(@PathVariable Integer id){
        Boolean success = permissionService.deletePermission(id);
        return ResponseEntity.ok(success);
    }

    @PostMapping("/addNewPermission/{parentId}")
    public ResponseEntity<?> addNewPermission(@PathVariable Integer parentId, @RequestBody SysPermission permission){
        Boolean success = permissionService.addNewPermission(parentId,permission);
        return ResponseEntity.ok(success);
    }

    @PostMapping("/editPermission/{id}")
    public ResponseEntity<?> editPermission(@PathVariable Integer id,@RequestBody SysPermission permission){
        Boolean success = permissionService.editPermission(id,permission);
        return ResponseEntity.ok(success);
    }

}
